utils = {}

function utils.AddVariable(vindex, vname, vlable, vtype)
    local Variable = {
        Index = vindex,
        Name  = vname,
        Label = vlable,
        --Value = "",
        Type  = vtype,
    }
    --print(variable)
    return Variable
end

function utils.AppendVariable(vindex, vname, vValue, vExplain)
    local Variable = {
        Index   = vindex,
        Name    = vname,
        Label   = "",
        Value   = vValue,
        Explain = vExplain,
        Type    = "",
    }
    --print(variable)
    return Variable
end

function utils.And(num1, num2)
    local tmp1 = num1
    local tmp2 = num2
    local ret = 0
    local count = 0
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 and s1 == 1 then
            ret = ret + 2 ^ count
        end
        tmp1 = math.modf(tmp1 / 2)
        tmp2 = math.modf(tmp2 / 2)
        count = count + 1
    until (tmp1 == 0 and tmp2 == 0)
    return ret
end

function utils.Not(num, len)
    local tmp1 = num
    local ret = 0
    local count = 0
    for index = 1, len, 1 do
        local s1 = math.modf(tmp1 % 2)
        if s1 == 0 then
            ret = ret + 2 ^ count
        end
        tmp1 = math.modf(tmp1 / 2)
        count = count + 1
    end
    return ret
end

function utils.Hex2Str(buf, bufLen)
    local ret = {}
    local low
    local high
    --local str

    for index = 1, bufLen, 1 do
        if (math.modf(buf[index] / 16) >= 0x0A) then
            high = math.modf(buf[index] / 16) + 0x37
        elseif (math.modf(buf[index] / 16) < 0x0A) then
            high = math.modf(buf[index] / 16) + 0x30
        end

        if (math.modf(buf[index] % 16) >= 0x0A) then
            low = math.modf(buf[index] % 16) + 0x37
        elseif (math.modf(buf[index] % 16) < 0x0A) then
            low = math.modf(buf[index] % 16) + 0x30
        end

        ret[2 * index - 1] = high
        ret[2 * index] = low
        --str = string.format("%02X",ret[2*index-1])
        --print(str)
        --str = string.format("%02X",ret[2*index])
        --print(str)
        index = index + 1
    end

    return ret
end

function utils.Str2Hex(buf, bufLen)

    local ret = {}
    local high = 0
    local low = 0
    --local str
    --数据长度必须是偶数
    if (math.modf(bufLen % 2) == 0)
    then
        for index = 1, bufLen / 2, 1 do
            if (buf[2 * index - 1] >= 0x30 and buf[2 * index - 1] <= 0x39) then
                high = math.modf((buf[2 * index - 1] - 0x30) * 16)
            elseif (buf[2 * index - 1] >= 0x41 and buf[2 * index - 1] <= 0x5A) then
                high = math.modf((buf[2 * index - 1] - 0x37) * 16)
            end

            if (buf[2 * index] >= 0x30 and buf[2 * index] <= 0x39) then
                low = math.modf((buf[2 * index] - 0x30))
            elseif (buf[2 * index] >= 0x41 and buf[2 * index] <= 0x5A) then
                low = math.modf((buf[2 * index] - 0x37))
            end

            ret[index] = high + low
            --str = string.format("%02X",ret[index])
            --print(str)
            index = index + 1
        end
    end

    return ret
end

function utils.Copy(buf, index, bufLen)
    local tmp = {}

    for i = 0, bufLen, 1 do
        table.insert(tmp, buf[i + index])
    end

    return tmp
end

--获取指定key在table中的索引
function utils.Indexof(table, key) --
    for i = 1, #table do
        if (key == table[i])
        then
            return i
        end
    end
    return nil
end

--根据指定key在table中的索引，获取该索引在value中的对应值
function utils.ValueOfKey(tab, key, value) --
    return value[utils.Indexof(tab, key)]
end

return utils
